﻿CREATE PROCEDURE [dbo].[sp_AppendToFile](@FileName varchar(255), @Text1 nvarchar(max)) AS 
DECLARE @FS int, @OLEResult int, @FileID int 


EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT 
IF @OLEResult <> 0 PRINT 'Scripting.FileSystemObject : ' 

--Open a file 
execute @OLEResult = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 8, 1 
IF @OLEResult <> 0 exec DisplayOAErrorInfo @FS,@OLEResult 

--Write Text1 
execute @OLEResult = sp_OAMethod @FileID, 'WriteLine', Null, @Text1 
IF @OLEResult <> 0 exec DisplayOAErrorInfo @FileID,@OLEResult 

EXECUTE @OLEResult = sp_OADestroy @FileID 
EXECUTE @OLEResult = sp_OADestroy @FS 



